package edu.neumont.pro180._2009_q2;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.Border;


public class GUI extends JFrame {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1479418810734845899L;
	private int height = 8;
    private int width = 8;

    private JPanel panel;
    private JPanel chessBoard;
    private JLabel square;
    private JPanel squarePanel;

    private JMenuBar menuBar;
    private JMenu file;
    private JMenuItem exit;

    private JButton startGame;
    private JPanel rButtonPanel;
    private JRadioButton black;
    private JRadioButton white;

    public GUI() {
	super("Chess");


	this.panel = new JPanel();
	this.chessBoard = new JPanel();

	this.menuBar = new JMenuBar();
	this.file = new JMenu("File");
	this.exit = new JMenuItem("Exit");

	this.startGame = new JButton("Start Game");
	this.rButtonPanel = new JPanel();
	this.black = new JRadioButton("Black");
	this.white = new JRadioButton("White");

	//add action listener
	this.exit.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent arg0) {
		System.exit(0);
	    }});

	//set for panel
	this.panel.setLayout(new GridLayout(1,2));
	this.chessBoard.setMinimumSize(new Dimension(120,120));
	Border edge = BorderFactory.createLineBorder(Color.BLACK);
	this.chessBoard.setBorder(edge);
	this.chessBoard.setLayout(new GridLayout(8,8));

	//add to stuff
	this.add(panel);
	this.file.add(exit);
	this.menuBar.add(file);
	this.add(chessBoard);

	buttons();
	createSquares();

	//set stuff for window
	this.setLayout(new FlowLayout());
	this.setJMenuBar(menuBar);
	this.setDefaultCloseOperation(EXIT_ON_CLOSE);
	this.setMinimumSize(new Dimension(600,400));
	this.pack();
    }

    private void createSquares() {
	Color color = Color.GREEN;
	Border squareBorder = BorderFactory.createLineBorder(Color.BLACK);
	for(int i = 0; i < height; i++){
	    for(int j = 0; j < width; j++) {
		squarePanel = new JPanel();
		square = new JLabel();
		squarePanel.add(square);
		if((i+j)%2 == 0) {
		    color = Color.DARK_GRAY;
		} else {
		    color = Color.WHITE;
		}
		squarePanel.setBackground(color);
		square.setForeground(Color.PINK);
		squarePanel.setBorder(squareBorder);
		chessBoard.add(squarePanel);
	    }
	}

	//populate with pieces
	for(int i = 0; i < height; i++) {
	    for(int j = 0; j < width; j++) {

//		int curN = (i * height) + j;

//		Piece curPiece = this.myBoard.get(i, j);
//		JLabel label = (JLabel)((JPanel)this.chessBoard.getComponent(curN)).getComponent(0);
//
//		if(curPiece != null) {
//		    label.setText(curPiece.toString());
//		}
	    }
	}
    }

    private void buttons() {
	this.add(rButtonPanel);
	this.rButtonPanel.setLayout(new GridLayout(5,1));
	this.rButtonPanel.add(black);
	this.rButtonPanel.add(white);
	this.rButtonPanel.add(startGame);

	ButtonGroup rGroup = new ButtonGroup();
	rGroup.add(black);
	rGroup.add(white);

	this.startGame.addActionListener(new ButtonListener());
    }

    private class ButtonListener implements ActionListener {

	public void actionPerformed(ActionEvent e) {
	    boolean validGameStart = false;
	    if(black.isSelected()) {
		validGameStart = true;

	    } else if(white.isSelected()) {
		validGameStart = true;
		
	    } else {
		JOptionPane.showMessageDialog(null,"You need to select how to play, sillster.");
	    }

	    if(validGameStart) {
		if(startGame.getText().equals("Start Game")) {
		    startGame.setText("Restart");
		} else {
		    startGame.setText("Start Game");
		}
	    }
	}
    }
    
    public static void main(String[] args) {
		GUI gui = new GUI();
		gui.setVisible(true);
    }

}
